package com.crm.controller;

import com.crm.result.Result;
import com.crm.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@Slf4j
@RequestMapping("/common/upload")
@Api(tags = "文件上传")
public class UploadController {
    @Autowired
    private AliOssUtil aliOssUtil;

    @PostMapping
    @ApiOperation("文件上传")
    public Result upload(MultipartFile file) throws IOException {
        //获取原始文件名
        String originalFilename = file.getOriginalFilename();
        //截取文件后缀名
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        //使用uuid生成不重复的文件名
        String fileName = UUID.randomUUID().toString() + extension;
        //使用工具类，调用方法，上传文件
        String pathLoad = aliOssUtil.upload(file.getBytes(), fileName);
        return Result.success(pathLoad);
    }

}
